/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.bricket.web.common;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.odlabs.wiquery.ui.themes.ThemeUiHelper.IconEnum;

public abstract class BricketAjaxButton extends BricketAjaxPanel {
	public BricketAjaxButton(StringResourceModel label, Form<?> form) {
		this(label, null, form);
	}
	
	public BricketAjaxButton(StringResourceModel label, IconEnum icon, Form<?> form) {
		super(BricketPanel.BUTTON_ID);
		AjaxButton ajaxButton = new AjaxButton("button", form) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				BricketAjaxButton.this.onSubmit(target, form);
			}
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				BricketAjaxButton.this.onError(target, form);
			};
		};
		ajaxButton.add(new AttributeModifier("class", true, new Model<String>(BUTTON_BASE_CLASS + (icon != null ? BUTTON_TEXT_ICON_CLASS : BUTTON_TEXT_ONLY_CLASS))));
		
		Label iconLabel = new Label("icon", "");
		iconLabel.add(new AttributeModifier("class", true, new Model<String>(BUTTON_ICON_BASE_CLASS + (icon != null ? icon.getCssClass() : ""))));
		ajaxButton.add(iconLabel.setVisible(icon != null));

		Label buttonLabel = new Label("label", label);
		buttonLabel.add(new AttributeModifier("class", true, new Model<String>(BUTTON_TEXT_CLASS)));
		ajaxButton.add(buttonLabel);
		
		add(ajaxButton);
	}

	protected abstract void onSubmit(AjaxRequestTarget target, Form<?> form);

	protected abstract void onError(AjaxRequestTarget target, Form<?> form);
}
